/**
 * Created by shitao on 15/10/17.
 */

(function () {
    // 创建service对象
    angular.module('my-tabs.services')
        .factory('exploreService', exploreService);
    // 申明依赖
    exploreService.$inject = ['exploreApi', 'sessionService'];

    function exploreService(exploreApi, session) {

        var categories = [],
            promotions = [],
            stores = [];

        return {

            loadCategories: function () {
                return exploreApi.getCategories()
                    .success(function (items) {
                        categories.length = 0;
                        _.each(items, function (item) {
                            categories.push(item);
                        })
                    });
            },

            categories: function () {
                return categories;
            },

            getCategory: function (categoryId) {
                for (var i = 0; i < categories.length; i++) {
                    if (categories[i].id === parseInt(categoryId)) {
                        return categories[i];
                    }
                }
                return null;
            },

            loadPromotions: function () {
                return exploreApi.getPromotions()
                    .success(function (items) {
                        promotions.length = 0;
                        _.each(items, function (item) {
                            promotions.push(item);
                        })
                    });
            },

            promotions: function () {
                return promotions;
            },

            getPromotion: function (promId) {
                for (var i = 0; i < promotions.length; i++) {
                    if (promotions[i].id === parseInt(promId)) {
                        return promotions[i];
                    }
                }
                return null;
            },

            loadStores: function () {
                return exploreApi.getStores()
                    .success(function (items) {
                        stores.length = 0;
                        _.each(items, function (item) {
                            stores.push(item);
                        })
                    });
            },

            stores: function () {
                return stores;
            },

            getStore: function (storeId) {
                for (var i = 0; i < stores.length; i++) {
                    if (stores[i].id === parseInt(storeId)) {
                        return stores[i];
                    }
                }
                return null;
            }

        };
    };
})();
